package com.源码分析;

import org.junit.Test;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import reactor.core.publisher.Flux;

// doOn系列方法是publisher的同步钩子方法，在subscriber触发一系列事件的时候触发
public class DoonDemo {
	static final Logger LOGGER = LoggerFactory.getLogger(DoonDemo.class);

	@Test
	public void testDoOnMethods() {
		Flux.range(1, 3)
			.doOnSubscribe(e -> LOGGER.info("doOnSubscribe:{}", e)).doOnEach(e -> LOGGER.info("doOnEach:{}", e))
			.doOnError(e -> LOGGER.info("doOnError:{}", e)).doOnNext(e -> LOGGER.info("doOnNext:{}", e))
			.doOnRequest(e -> LOGGER.info("doOnRequest:{}", e)).doOnTerminate(new Runnable() {
				@Override
				public void run() {
					LOGGER.info("doOnTerminate");
				}
			}).doOnCancel(new Runnable() {
				@Override
				public void run() {
					LOGGER.info("doOnCancel");
				}
			}).doOnComplete(new Runnable() {
				@Override
				public void run() {
					LOGGER.info("doOnComplete");
				}
			}).subscribe(new Subscriber<Integer>() {
				@Override
		        public void onSubscribe(Subscription s) {
		            LOGGER.info("request max");
		            s.request(Integer.MAX_VALUE);
		        }

		        @Override
		        public void onNext(Integer integer) {
		            LOGGER.info("get data:{}",integer);
		        }

		        @Override
		        public void onError(Throwable t) {

		        }

		        @Override
		        public void onComplete() {
		            LOGGER.info("onComplete");
		        }
			});
	}

}
